//
//  QTUserInfoViewController.m
//  qdqts
//
//  Created by centling on 14-6-21.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTUserInfoViewController.h"
#import "QTUserInfoTableViewCell.h"
#import "QTUserDefault.h"
#import "QTClient.h"
#import "QTLoginViewController.h"
#import "QTUserDefault.h"
#import "Global.h"
//#import "QTAppDelegate.h"

@implementation QTUserInfoViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"个人中心";
    selectedViewController=self.navigationController;
    
    self.navigationItem.hidesBackButton = YES;
    [self getCurrentTime];
    
    QTUserDefault *userDefault=[QTUserDefault sharedInstance];
    
    NSLog(@"%@",[userDefault readNSUserDefaults:@"puid"]);
    [[QSClient instance]loadUserInfo:[userDefault readNSUserDefaults:@"puid"] completionBlock:^(UserInfo *userInfo, NSError *error){
        NSLog(@"%@",userInfo);
        self.userInfo = userInfo;
        NSLog(@"%@",self.userInfo.valueMap.loginName);
        
        [self showUserInfo];
        [self.tableView reloadData];
        
    }];
    
    self.view.backgroundColor=GRAYCOLOR;
    _tableView.scrollEnabled=NO;
//    //UIView设置阴影
//    [_tableView.layer setShadowOffset:CGSizeMake(0.5, 0.5)];
//    [_tableView.layer setShadowRadius:2];
//    [_tableView.layer setShadowOpacity:0.1];
//    [_tableView.layer setShadowColor:[UIColor blackColor].CGColor];
    //UIView设置边框
    [_tableView.layer setCornerRadius:5];
    [_tableView.layer setBorderWidth:1];
    [_tableView.layer setBorderColor:GRAYCOLOR.CGColor];

    
    //Button设置圆角
    [_exitButton.layer setMasksToBounds:YES];
    [_exitButton.layer setCornerRadius:3];
    // Do any additional setup after loading the view from its nib.
}



- (void)showUserInfo
{
    self.userLabel.text = self.userInfo.valueMap.loginName;
    switch (self.userInfo.valueMap.userType) {
        case 1:
            self.userTypeLabel.text = @"普通用户";
            break;
        case 2:
            self.userTypeLabel.text = @"政府用户";
            break;
        case 3:
            self.userTypeLabel.text = @"企业用户";
            break;
            
        default:
            self.userTypeLabel.text = @"普通用户";
            break;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"QTUserInfoTableViewCell";
    QTUserInfoTableViewCell *cell=(QTUserInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell ==nil) {
        //加载MyCell.xib文件，此处loadNibNamed后面的参数CellIdentifier必须与MyCell.xib文件名相同，否则会无法加载，报错崩溃
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"QTUserInfoTableViewCell" owner:self options:nil];
        cell = (QTUserInfoTableViewCell *)[nibArray objectAtIndex:0];
        
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    switch (indexPath.row) {
        case 0:
            cell.keyLabel.text = [NSString stringWithFormat:@"用户名称"];
            cell.valueLabel.text = self.userInfo.valueMap.name;
            break;
        case 1:
            cell.keyLabel.text = [NSString stringWithFormat:@"职位"];
            cell.valueLabel.text = self.userInfo.valueMap.position;
            break;
        case 2:
            cell.keyLabel.text = [NSString stringWithFormat:@"电话"];
            cell.valueLabel.text = self.userInfo.valueMap.telNumber;
            break;
        case 3:
            cell.keyLabel.text = [NSString stringWithFormat:@"Email"];
            cell.valueLabel.text = self.userInfo.valueMap.userEmail;
            break;
            
        default:
            break;
    }
    return cell;
}



- (IBAction)exitClick:(id)sender {
    QTUserDefault *userDefault=[QTUserDefault sharedInstance];
//    [userDefault saveNSUserDefaults:@"" forKey:@"userName"];
    [userDefault saveNSUserDefaults:@"" forKey:@"passwd"];
    [userDefault saveNSUserDefaults:@"" forKey:@"puid"];
    [self showAlert];
}

-(void)showAlert
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                  message:[NSString stringWithFormat:@"确定要退出？",nil]
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:@"取消", nil];
    [alert show];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        QTLoginViewController *loginViewController=[[QTLoginViewController alloc] init];
        [self.navigationController pushViewController:loginViewController animated:YES];
        
    }
    
}

//- (IBAction)exit:(id)sender {
//    QTAppDelegate *app = [UIApplication sharedApplication].delegate;
//    UIWindow *window = app.window;
//
//    [UIView animateWithDuration:1.0f animations:^{
//        window.alpha = 0;
//        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
//    } completion:^(BOOL finished) {
//        exit(0);
//    }];
//}
-(void)getCurrentTime
{
    NSDate *date = [NSDate date];
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSDateComponents *comps;
    comps = [calender components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    self.currentTime = [NSString stringWithFormat:@"%d-%d-%d",year, month, day];
    
}
@end
